package com.myl.admin.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * SpringDocConfig
 * author: myl <ixiaomu@qq.com> 2023/12/19
 * tips:
 */
@Slf4j
@Configuration
public class SwaggerConfig {


    /**
     * 是否开启swagger
     */
    @Value("${springdoc.swagger-ui.enabled}")
    private boolean enabled;

    @Bean
    public OpenAPI springShopOpenAPI() {
        final String securitySchemeName = "bearerAuth";

        return new OpenAPI()
                .info(
                        new Info().title("MYL Web API")
                                .description("请描述一下这个应用")
                                .version("v1.0.1")
                                .license(new License().name("Apache 2.0").url("http://springdoc.org"))
                                .contact(new Contact().name("MYL").email("ixiaomu@qq.com").url("http://www.myl.com"))
                )
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"))
                .components(
                        new Components().securitySchemes(securitySchemes())
                );
    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private Map<String, SecurityScheme> securitySchemes() {
        Map<String, SecurityScheme> apiKeyList = new HashMap<>();
        apiKeyList.put("Authorization", new SecurityScheme().name("ABC").type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER).description("Authorization")
        );
        return apiKeyList;
    }

    @Bean
    public GroupedOpenApi defaultOpenApi() {
        // 扫描URL
        String[] paths = {"/**"};
        // 排除的URL
        String[] excludePaths = {"/sys/**", "/portal/**"};
        return GroupedOpenApi.builder()
                .group("default")
                .displayName("公共接口")
                .pathsToExclude(excludePaths)
                .pathsToMatch(paths)
                .build();
    }

    @Bean
    public GroupedOpenApi systemOpenApi() {
        String[] paths = {"/sys/**"};
        return GroupedOpenApi.builder()
                .group("system")
                .displayName("系统")
                .pathsToMatch(paths)
                .build();
    }

    @Bean
    public GroupedOpenApi portalOpenApi() {
        String[] paths = {"/portal/**"};
        return GroupedOpenApi.builder()
                .group("portal")
                .displayName("门户")
                .pathsToMatch(paths)
                .build();
    }


}
